
﻿using System;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Threading;

using UFSoft.UBF.UI.WebControls;
using UFSoft.UBF.UI.Controls;
using UFSoft.UBF.Util.Log;
using UFSoft.UBF.Util.Globalization;

using UFSoft.UBF.UI.IView;
using UFSoft.UBF.UI.Engine;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.ActionProcess;
using UFSoft.UBF.UI.WebControls.ClientCallBack;
using UFIDA.U9.UI.PDHelper;
using UFSoft.UBF.Util.DataAccess;
using System.Collections.Generic;
using MF.HSD.CreateVoucherBP.Proxy;



/***********************************************************************************************
 * Form ID: 
 * UIFactory Auto Generator 
 ***********************************************************************************************/
namespace MF.HSD.MFHSDCreateVoucherUIModel
{
    public partial class MFHSDCreateVoucherUIFormWebPart
    {
        #region Custome eventBind



        private void BtnOk_Click_Extend(object sender, EventArgs e)
        {
            this.Model.ClearErrorMessage();

            List<string> list = new List<string>();
            CreateVoucherProxy proxy = new CreateVoucherProxy();
            proxy.VoucherDTO = new List<CreateVoucherBP.VoucherDTOData>();

            foreach (SrcVoucherModelRecord rd in this.Model.SrcVoucherModel.SelectRecords)
            {
                if (!list.Contains(rd.VoucherNo))
                {
                    list.Add(rd.VoucherNo);
                }
            }
            foreach (string docno in list)
            {
                decimal moneyDr = 0, moneyCr = 0;
                CreateVoucherBP.VoucherDTOData data = new CreateVoucherBP.VoucherDTOData();
                data.VoucherNo = docno;          
                data.ToOrgCode = PDContext.Current.OrgRef.CodeColumn;
                data.VoucherLineDTO = new List<CreateVoucherBP.VoucherLineDTOData>();
                foreach (SrcVoucherModelRecord rd in this.Model.SrcVoucherModel.SelectRecords)
                {
                    if (docno == rd.VoucherNo)
                    {
                        data.OrgCode = rd.SrcOrgCode;                        
                        CreateVoucherBP.VoucherLineDTOData lineDto = new CreateVoucherBP.VoucherLineDTOData();
                        lineDto.LineNum = rd.LineNum;
                        //借贷金额反向
                        lineDto.MoneyDr = (decimal)rd.MoneyCr;
                        lineDto.MoneyCr = (decimal)rd.MoneyDr;
                        lineDto.AccountCode = rd.ToAccount;
                        //lineDto.Account = rd.Account;
                        lineDto.SegmentNumber = rd.SegmentNumber;
                        lineDto.SegmentValue = rd.SegmentValue;
                        moneyDr += (decimal)rd.MoneyDr;
                        moneyCr += (decimal)rd.MoneyCr;
                        data.VoucherLineDTO.Add(lineDto);
                    }
                }
                //值大于0 ，表示借方，小于0 表示创建到贷方
                data.TotalMoney = moneyDr - moneyCr;
                proxy.VoucherDTO.Add(data);
            }
            if (proxy.VoucherDTO.Count > 0)
            {
                proxy.Do();
            }
            this.CloseDialog(true);
        }

        //BtnClose_Click...
        private void BtnClose_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnClose_Click_DefaultImpl(sender, e);
        }


        public DataTable GetVoucherDate(string orgcode)
        {
            //执行存储过程
            DataParamList paramList = new DataParamList();
            paramList.Add(DataParamFactory.CreateInput("OrgCode", orgcode, DbType.String));
            DataSet ds = new DataSet();
            DataAccessor.RunSP("Cust_Yonyou_MFHSDDealAccount", paramList, out ds);
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }


        #region 自定义数据初始化加载和数据收集
        private void OnLoadData_Extend(object sender)
        {
            //if (this.Model.SrcVoucherModel.FocusedRecord == null)
            //{
            //    this.Model.SrcVoucherModel.AddNewUIRecord();

            //}
            DataTable dt = GetVoucherDate(PDContext.Current.OrgRef.CodeColumn);
            if (dt != null)
            {
                this.Model.SrcVoucherModel.Records.Clear();
                foreach (DataRow dr in dt.Rows)
                {
                    SrcVoucherModelRecord rd = this.Model.SrcVoucherModel.AddNewUIRecord();
                    rd.SrcOrgCode = dr["FromOrg"].ToString();
                    rd.SrcOrgName = dr["FromOrg"].ToString();
                    rd.VoucherNo = dr["VoucherDisplayCode"].ToString();
                    rd.LineNum = dr["SerialNo"].ToString();
                    rd.Account = dr["AccountCode"].ToString();
                    rd.SegmentNumber = dr["ToSegmentNumber"].ToString();
                    rd.SegmentValue = dr["ToSegmentValue"].ToString();
                    rd.AccountCode = dr["FromAccountCode"].ToString();
                    rd.ToAccount = dr["ToAccountCode"].ToString();
                    rd.MoneyDr = decimal.Parse(dr["AccountedDr"].ToString());
                    rd.MoneyCr = decimal.Parse(dr["AccountedCr"].ToString());
                    rd.AccountName = dr["AccountName"].ToString();
                    rd.Abstracts = dr["Abstracts"].ToString();
                }
            }
            OnLoadData_DefaultImpl(sender);
        }
        private void OnDataCollect_Extend(object sender)
        {
            OnDataCollect_DefaultImpl(sender);
        }
        #endregion

        #region 自己扩展 Extended Event handler 
        public void AfterOnLoad()
        {

        }

        public void AfterCreateChildControls()
        {



        }

        public void AfterEventBind()
        {
        }

        public void BeforeUIModelBinding()
        {

        }

        public void AfterUIModelBinding()
        {
            this.DataGrid0.ReadOnly = true;

        }


        #endregion

        #endregion

    }
}